﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetServiceLib
{
    public class ArgumentParser
    {
        public static object Try(string argument, string type) {
            if (type == "System.String")
                return argument;

            object result = argument;
            if (type == "System.Int16")
            {
                Int16 i;
                if (Int16.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.Int32") 
            {
                Int32 i;
                if (Int32.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.Int64")
            {
                Int64 i;
                if (Int64.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.UInt16")
            {
                UInt16 i;
                if (UInt16.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.UInt32") 
            {
                UInt32 i;
                if (UInt32.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.UInt64")
            {
                UInt64 i;
                if (UInt64.TryParse(argument, out i))
                {
                    result = i;
                }
            }
            else if (type == "System.Boolean")
            {
                Boolean b;
                if (Boolean.TryParse(argument, out b))
                {
                    result = b;
                }
            }
            else if (type == "System.DateTime")
            {
                DateTime dt;
                if (DateTime.TryParse(argument, out dt))
                {
                    result = dt;
                }
            }
            else if (type == "System.Byte")
            {
                Byte b;
                if (Byte.TryParse(argument, out b))
                {
                    result = b;
                }
            }
            else if (type == "System.Single") // single-precision floating-point number
            {
                // float parse is not working correctly.
                // following code works:
                //      string strFloat = (3.40282e+038f).ToString(System.Globalization.CultureInfo.InvariantCulture);
                //      float numFloat = float.Parse(strFloat, System.Globalization.CultureInfo.InvariantCulture);
                // following code throws an exception:
                //      string strFloat = ("3.40282e+038f").ToString(System.Globalization.CultureInfo.InvariantCulture);
                //      float numFloat = float.Parse(strFloat, System.Globalization.CultureInfo.InvariantCulture);
                Single s;
                if (Single.TryParse(argument, out s))
                {
                    result = s;
                }
            }
            else if (type == "System.Double")
            {
                Double d;
                if (Double.TryParse(argument, out d))
                {
                    result = d;
                }
            }
            else if (type == "System.Char")
            {
                Char c;
                if (Char.TryParse(argument, out c))
                {
                    result = c;
                }
            }
            return result;
        }
    }
}
